package fwhmc.utils;

import fdupc.http.ResultBean;
import net.sf.json.JSONObject;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.netty.ByteBufMono;

/**
 * 类描述:  数据数据工具
 *
 * @author 王雷
 * @version 1.0.0
 * @date 2022/10/1 0001 下午 6:58
 */
public class OutDataTools {

    /**
     * 功能描述：统一后台流输出处理
     *
     * @param serverWebExchange Web服务
     * @param resultBean        返回的数据
     * @return 输出对象
     * @date : 2022/10/2 0002 上午 11:20
     */
    public static Mono<Void> giveResultBeanWriteAndFlushWith(ServerWebExchange serverWebExchange, ResultBean resultBean) {
        ServerHttpResponse response = serverWebExchange.getResponse();
        response.setStatusCode(HttpStatus.OK);
        final DataBuffer write = response.bufferFactory().allocateBuffer().write(JSONObject.fromObject(resultBean)
                .toString().getBytes());
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        return response.writeAndFlushWith(Mono.just(ByteBufMono.just(write)));
    }
}
